import { MacroDefine } from "./Support/Share/Global/MacroDefine";

export class ClientUtil {

    static Init() {
        let url = "resources/cacert.pem";
        url = ClientUtil.TransformURL(url);

        MacroDefine.ET_SERVER = false;
        MacroDefine.Protocols = null;
        MacroDefine.CAUrl = url;
    }

    static TransformURL(url: string) {
        let uuid = cc.assetManager.utils.getUuidFromURL(url);
        if (!uuid) {
            return url;
        }
        let bundle = cc.assetManager.bundles.find((b) => {
            return !!b.getAssetInfo(uuid);
        });
        if (!bundle) {
            return url;
        }
        let hashValue = '';
        let info = bundle.getAssetInfo(uuid);
        if (url.startsWith(bundle.base + bundle["_config"].nativeBase)) {
            hashValue = info.nativeVer || '';
        } else {
            hashValue = info.ver || '';
        }
        if (!hashValue || url.indexOf(hashValue) !== -1) {
            return url;
        }
        let hashPatchInFolder = false;
        if (cc.path.extname(url) === '.ttf') {
            hashPatchInFolder = true;
        }
        if (hashPatchInFolder) {
            let dirname = cc.path.dirname(url);
            let basename = cc.path.basename(url);
            url = `${dirname}.${hashValue}/${basename}`;
        } else {
            url = url.replace(/.*[/\\][0-9a-fA-F]{2}[/\\]([0-9a-fA-F-]{8,})/, (match, uuid) => {
                return match + '.' + hashValue;
            });
        }
        return url;
    }

}